﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Serialization;
using System.IO;

public class XMLConfig : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        var temp = new Test1() { id = 1, name = "神张", height = 1.71f };
        temp.name = "嘿嘿";
        //temp.content.Add(1, "老张");
        //temp.content.Add(2, "老王");
        temp.list.Add("老张");
        temp.list.Add("老王");

        Serialize<Test1>(temp);
        Debug.Log(Deserialize<Test1>());
    }

    T Deserialize<T>()
    {
        using (FileStream fs = File.OpenRead("Test1.xml"))
        {
            XmlSerializer xmldes = new XmlSerializer(typeof(T));
            return (T)xmldes.Deserialize(fs);
        }
    }

    void Serialize<T>(object obj)
    {
        XmlSerializer xml = new XmlSerializer(typeof(T));
        using (FileStream fs = File.OpenWrite("Test1.xml"))
        {
            xml.Serialize(fs, obj);
        }
        
    }
}


public class Test1
{
    public int id;
    public string name;
    public float height;
    public char sex;
    public List<string> list = new List<string>();

    public override string ToString()
    {
        return string.Format("id :{0}, name:{1}, height:{2}, sex::{3}, list.count:{4}", id, name, height, sex, list.Count);
    }
}

public class Player
{
    public int playerID;
    public string name;
}

public class PlayerConfig
{
    public Dictionary<int, Player> players;
}